﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDCRNewEdit.Model;
using System.Reflection;

namespace LDCRNewEdit.UI.地图编辑.场景地图设计
{
    public partial class SceneBuildViewForm : Form
    {
        private SceneBuildLv build;

        public SceneBuildViewForm()
        {
            InitializeComponent();

            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.build.PlayMode = (byte)this.comboBox1.SelectedIndex;
            build.FrameNum = byte.Parse(this.textBoxFrameNum.Text);
            build.FrameDelay = byte.Parse(this.textBoxFrameDelay.Text);
            build.KeyPointX = short.Parse(this.textBoxKeyPointX.Text);
            build.KeyPointY = short.Parse(this.textBoxKeyPointY.Text);
            build.X = short.Parse(this.textBoxX.Text);
            build.Y = short.Parse(this.textBoxY.Text);
            build.Path = textBoxPath.Text;
            build.Image = this.pictureBox1.Image;

            build.W = (short)build.Image.Width;
            build.H = (short)build.Image.Height;

            this.Close();
        }

        public SceneBuildLv View(SceneBuildLv build)
        {
            this.build = build;

            this.comboBox1.SelectedIndex = build.PlayMode;
            this.textBoxFrameNum.Text = build.FrameNum.ToString();
            this.textBoxFrameDelay.Text = build.FrameDelay.ToString();
            this.textBoxKeyPointX.Text = build.KeyPointX.ToString();
            this.textBoxKeyPointY.Text = build.KeyPointY.ToString();
            this.textBoxX.Text = build.X.ToString();
            this.textBoxY.Text = build.Y.ToString();
            this.textBoxPath.Text = build.Path;

            this.label1LevelSize.Text = build.Image.Width + "," + build.Image.Height;

            this.pictureBox1.Image = build.Image;

            //UpdateKeyPoint();

            if (this.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return this.build;
            }
            else
            {
                return null;
            }
        }

        private void UpdateKeyPoint()
        {
            int originalWidth = this.pictureBox1.Image.Width;  
            int originalHeight = this.pictureBox1.Image.Height;  
  
            PropertyInfo rectangleProperty = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);  
            Rectangle rectangle = (Rectangle)rectangleProperty.GetValue(this.pictureBox1, null);  
  
            int currentWidth = rectangle.Width;  
            int currentHeight = rectangle.Height;  
  
            double rate = (double)currentHeight / (double)originalHeight; 

            Graphics g = Graphics.FromImage(this.pictureBox1.Image);
            g.DrawLine(new Pen(Color.Blue), new Point((int)(this.build.KeyPointX * rate),0), new Point((int)(this.build.KeyPointX * rate),this.pictureBox1.Size.Height));
            g.DrawLine(new Pen(Color.Blue), new Point(0,(int)(this.build.KeyPointY * rate)), new Point(this.pictureBox1.Size.Width,(int)(this.build.KeyPointY * rate)));
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }
    }
}
